
// 初始化 Pomelo 并进入相应的场景
function authEntry(host, port, uid, token, callback, areaId) {
  _queryEntry(uid, function(host, port) {
    _entry(host, port, token, callback);
  });

  function _queryEntry(uid, callback) {
    var gateHost = host;
    var gatePort = port;
    pomelo.init({host: gateHost, port: gatePort, log: true}, function() {
      pomelo.request('gate.gateHandler.queryEntry', {uid: uid}, function(data) {
        pomelo.disconnect();

        if(data.code === 2001) {
          alert('Servers error!');
          return;
        }
        callback(data.host, data.port);
      });
    });
  }

  // 登录 connector 及场景
  function _entry(host, port, token, callback) {
    // init socketClient
    // TODO for development
    pomelo.init({host: host, port: port, log: true}, function() {
      var msg = {token: token};
      if(areaId) {
        msg.areaId = areaId;
      }
      pomelo.request('connector.entryHandler.entry', msg, function(data) {
        var player = data.player;

        if (data.code == 1001) {
          callback('Login fail!');
          return;
        } else if (data.code == 1003) {
          callback('Username not exists!');
          return;
        }

        if (data.code != 200) {
          callback('Login Fail!');
          return;
        } 

        // 登录成功，接下来可以进行后续操作了
        callback(null, data);
      });
    });
  }
}

function enterScene(callback) {
  var route = area+'.playerHandler.enterScene';

  // 登录场景
  pomelo.request(route, {}, function(data) {
    console.log(route);
    callback(null, data);
  });
}

// 创建房间
function createRoom(id, game, areaId) {
  _confirmModal(_initCreate, '创建房间后，请在30分内登录并启动游戏，超时后房间将会解散');
  function _initCreate() {
    var url = '/game/paixing/'+id+'/'+game;
    $.get(url, function(data){
      if(data.code!==0) {
        mind.alert(data.message, 'error');
        return;
      }
      var set = data.set;
      var turl = '/game/player/'+set.creator+'/token';
      $.get(turl, function(result){
        if(result.code!==0) {
          mind.alert(result.message, 'error');
          return;
        }
        authEntry(host, port, set.creator, result.token, function(err, data) {
          if(err) {
            mind.alert(err, 'error');
            return;
          }
          if(data.code!==200) {
            mind.alert(data.msg||'操作失败', 'error');
            return; 
          }
          enterScene(function(err, data) {
            _createRoom(set.room, set.paixing);
          })
        }, areaId);
      });
    });
  }

  /**
   * room - 房间设置
   * paixing - 牌型设置
   */ 
  function _createRoom(room, paixing) {
    var route = area+'.'+game+'Handler.createRoom';

    room.paixing = paixing;
    pomelo.request(route, room, function(data) {
      if(data.code===200) {
        $.ajax({
          url: '/game/paixing/'+id,
          method: 'PUT',
          data: {roomId: data.rid},
          success: function(result) {
            pomelo.disconnect();
            window.location.href = window.location.href;
          }
        })
      } else {
        mind.alert(data.msg, 'error');
      }
    }) 
  }
}

